/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imageClipping;

import apiAccess.APIAccess;
import apiAccess.Product;
import hcitpe2.AppHCIStore;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import hcitpe2.frmPrincipal;
import ocr.OCRReader;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationContext;

/**
 *
 * @author Alan
 */


public class Clipping extends JPanel
{
    BufferedImage image;
    Dimension size;
    Rectangle clip;
    boolean showClip;
    frmPrincipal frame;
    JFrame parent;

    public Clipping(BufferedImage image,frmPrincipal f,JFrame parent)
    {
        this.parent = parent;
        this.image = image;
        frame = f;
        size = new Dimension(image.getWidth(), image.getHeight());
        showClip = false;

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int x = (getWidth() - size.width)/2;
        int y = (getHeight() - size.height)/2;
        g2.drawImage(image, x, y, this);
        if(showClip)
        {
            if(clip == null)
                createClip();
            g2.setPaint(Color.red);
            g2.draw(clip);
        }
    }

    public void setClip(int x, int y)
    {
        // keep clip within raster
        int x0 = (getWidth() - size.width)/2;
        int y0 = (getHeight() - size.height)/2;
        if(x < x0 || x + clip.width  > x0 + size.width ||
           y < y0 || y + clip.height > y0 + size.height)
            return;
        clip.setLocation(x, y);
        repaint();
    }

    @Override
    public Dimension getPreferredSize()
    {
        return size;
    }

    private void createClip()
    {
        clip = new Rectangle(140, 140);
        clip.x = (getWidth() - clip.width)/2;
        clip.y = (getHeight() - clip.height)/2;
    }

    public void createClip(int x, int y, int width, int height)
    {
        clip = new Rectangle(width, height);
        clip.x = x;
        clip.y = y;
        showClip = true;
    }

    private BufferedImage clipImage()
    {
        BufferedImage clipped = null;
        try
        {
            int w = clip.width;
            int h = clip.height;
            int x0 = (getWidth()  - size.width)/2;
            int y0 = (getHeight() - size.height)/2;
            int x = clip.x - x0;
            int y = clip.y - y0;
            clipped = image.getSubimage(x, y, w, h);
        }
        catch(RasterFormatException rfe)
        {
            System.out.println("raster format error: " + rfe.getMessage());
            return null;
        }

        return clipped;
        //JLabel label = new JLabel(new ImageIcon(clipped));
        //JOptionPane.showMessageDialog(this, label, "clipped image",
        //                              JOptionPane.PLAIN_MESSAGE);
    }

    public JPanel getUIPanel()
    {

        JLabel lblInstruction = new JLabel("Seleccione de izquierda a derecha el codigo de barras y presione Escanear");
        JButton btnClip = new JButton("Escanear");
        btnClip.setSize(150, 150);
        lblInstruction.setName("instruction");
        btnClip.setName("clip");
        btnClip.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                
                OCRReader ocr = new OCRReader(clipImage());

                int id = ocr.getProductID();
   
                if(id!=0){
                    try{
                        frame.setProductsList(new LinkedList<Integer>());
                        frame.getProductsList().add(id);
                        frame.setProduct(1);
                        parent.dispose();
                    }catch(Exception exp){
                        JOptionPane.showMessageDialog(null, "Codigo inexistente");
                    }
                }else
                    JOptionPane.showMessageDialog(null, "Codigo no detectado");
            }
        });


        ApplicationContext ctx= Application.getInstance().getContext();

        ctx.getResourceMap(AppHCIStore.class).injectComponent(lblInstruction);
        ctx.getResourceMap(AppHCIStore.class).injectComponent(btnClip);

        JPanel panel = new JPanel();
        panel.add(lblInstruction);
        panel.add(btnClip);
        return panel;
    }

   /* public static void main(String[] args) throws IOException
    {
        File file = new File("images/cougar.jpg");
        Cropping test = new Cropping(ImageIO.read(file));
        ClipMover mover = new ClipMover(test);
        test.addMouseListener(mover);
        test.addMouseMotionListener(mover);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(test));
        f.getContentPane().add(test.getUIPanel(), "South");
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}*/


}

